package com.yiqi.zhiyuan.feature.mine.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.luck.picture.lib.tools.PictureFileUtils;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.CacheManager;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.dialog.NormalDialog;
import com.suozhang.framework.widget.dialog.OnBtnClickL;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.vo.DiverCenterVO;
import com.yiqi.zhiyuan.feature.login.contract.LoginContract;
import com.yiqi.zhiyuan.feature.login.dependencies.login.DaggerLoginComponent;
import com.yiqi.zhiyuan.feature.login.dependencies.login.LoginPresenterModule;
import com.yiqi.zhiyuan.feature.login.view.EditPhoneActivity;
import com.yiqi.zhiyuan.feature.login.view.LoginActivity;
import com.yiqi.zhiyuan.feature.login.view.UpdateLoginPwdActivity;
import com.yiqi.zhiyuan.framework.api.UserApi;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongIM;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.mine.view
 * @ClassName: SettingActivity
 * @Description: java类作用描述
 * @Author:
 * @CreateDate: 2019/3/12 16:43
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/12 16:43
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SettingActivity extends BaseActivity implements LoginContract.View {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_setting_phone)
    TextView mTvSettingPhone;
    @BindView(R.id.tv_setting_cache)
    TextView mTvSettingCache;

    NormalDialog mDialog;


    @Inject
    LoginContract.Presenter mPresenter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initInjector() {
        DaggerLoginComponent.builder().loginPresenterModule(new LoginPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initDialog();
        initToolBar(mToolbar, getString(R.string.setting_title), true, true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getUserCenter();
    }

    @Override
    protected void initData() {
        try {
            mTvSettingCache.setText(CacheManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.rl_setting_update_phone, R.id.rl_setting_update_pwd, R.id.rl_setting_cache})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_setting_update_phone:
                startActivity(new Intent(this, EditPhoneActivity.class));
                break;
            case R.id.rl_setting_update_pwd:
                Intent intent=new Intent(this, UpdateLoginPwdActivity.class);
                intent.putExtra("user_info",mDiverCenterVO);
                startActivity(intent);
                break;
            case R.id.rl_setting_cache:

                NormalDialog normalDialogCache = new NormalDialog(this);
                normalDialogCache.content("确定清除缓存数据？");
                normalDialogCache.setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        try {
                            if (normalDialogCache != null) {
                                normalDialogCache.superDismiss();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        CacheManager.clearAllCache(SettingActivity.this);
                        PictureFileUtils.deleteCacheDirFile(SettingActivity.this);
                        try {
                            T.showShort("清除成功!");
                            normalDialogCache.dismiss();
                            String mDatas = CacheManager.getTotalCacheSize(SettingActivity.this);
                            if (!TextUtils.isEmpty(mDatas)) {
                                mTvSettingCache.setText(mDatas);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

                normalDialogCache.show();

                break;
            default:
                break;
        }
    }

    @OnClick(R.id.btn_setting_logout)
    public void onViewLogoutClicked() {
        if (mDialog != null) {
            mDialog.show();
        }


    }

    private void initDialog() {
        mDialog = new NormalDialog(this);
        mDialog.content(getString(R.string.dialog_setting_logout));
        mDialog.btnNum(2);
        mDialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                mDialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                mDialog.dismiss();
                mPresenter.logout();
            }
        });

    }

    @Override
    public void showGetCodeSuccess() {

    }

    @Override
    public void showGetCodeError(String errorMsg) {

    }

    @Override
    public void showLoginSuccess() {
        logout();
    }

    @Override
    public void showLoginError(String errorMsg) {
        showMsg(errorMsg);
        logout();
    }

    private void logout() {
        AM.user().loginOut();
        RongIM.getInstance().disconnect();
        RongIM.getInstance().logout();
        Intent outIntent = new Intent(SettingActivity.this,
                LoginActivity.class);
        outIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(outIntent);
    }
    DiverCenterVO mDiverCenterVO=null;
    private void getUserCenter() {
        AM.api().createApiService(UserApi.class)
                .driverCenter()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<DiverCenterVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DiverCenterVO diverCenterVO) {
                        mDiverCenterVO=diverCenterVO;
                        mTvSettingPhone.setText(diverCenterVO.getMobile());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
